﻿Shader "Dou/Unlit/GPUSkinnedMeshUnlit"
{
    Properties
    {
        _BaseMap ("Example Texture", 2D) = "white" {}
        _BaseColor ("Example Colour", Color) = (0, 0.66, 0.73, 1)
        _AnimTex ("AnimTex", 2D) = "white" {}
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline" = "UniversalPipeline"
            "RenderType" = "Opaque"
            "Queue" = "Geometry+300"
        }

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "GPUSkinnedMesh.hlsl"

        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        float4 _BaseColor;
        CBUFFER_END
        ENDHLSL

        Pass
        {
            Name "Unlit"
            Tags
            {
                "LightMode" = "UniversalForward"
            }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 4.5

            struct a2v
            {
                float4 posOS : POSITION;
                float2 uv : TEXCOORD0;
                float4 uv1 : TEXCOORD1;
                float4 uv2 : TEXCOORD2;
                float3 normalOS : NORMAL;
                float4 color : COLOR;
                uint instanceID : SV_InstanceId; // instanceID 实例在缓冲中的索引
            };

            struct v2f
            {
                float4 posCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normalWS : TEXCOORD1;
                float4 color : COLOR;
            };

            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);

            v2f vert(a2v v)
            {
                v2f o;
                
                float4x4 trans = transformBuf[v.instanceID];
                int frameIndex = playIndexBuf[v.instanceID];
                
                float4 pos = GetAnimPosition(trans, v.posOS, frameIndex, v.uv1, v.uv2);
                float4 normal = GetAnimPosition(trans, float4(v.normalOS, 0), frameIndex, v.uv1, v.uv2);
                
                o.posCS = TransformObjectToHClip(pos);
                o.normalWS = TransformObjectToWorld(normal);
                o.uv = v.uv;
                o.color = v.color;
                
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv);
                return baseMap * _BaseColor * i.color;
            }
            ENDHLSL
        }
    }
}
